﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using ASG.CodeGenerator.SQL;
using ASG.CodeGenerator.SQL.TSQL;

namespace ASG.CodeGenerator.UI.Desktop.TSQLTest
{
    public partial class MainForm : Form
    {
        private SQLServerCodeCollection _SQLServers = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            SQLServersLocator serversLocater = new SQLServersLocator();
            _SQLServers = serversLocater.GetServers();
            if (_SQLServers != null)
            {
                for (int i = 0; i < _SQLServers.Count; i++)
                {
                    cboServers.Items.Add(_SQLServers[i].Name);
                }
            }
            else
            {
                cboServers.Items.Add(".");
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            SQLServerCredentials cred = null;
            CredentialsForm form = new CredentialsForm();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                cred = form.Credentials;
            }
            if (cred != null)
            {
                List<SQLDatabaseCode> DatabasesInfo = _SQLServers[cboServers.SelectedIndex].GetDatabasesInfo(cred);
                cboDatabases.DataSource = DatabasesInfo;
            }
        }

        private void cboDatabases_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboDatabases.SelectedIndex > -1)
            {
                SQLDatabaseCode selectedDB = cboDatabases.SelectedItem as SQLDatabaseCode;
                lstTables.DataSource = selectedDB.Tables;
            }
        }
    }
}
